package team.sun.sss.controller.student;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import team.sun.sss.dao.staff.StaffDao;
import team.sun.sss.dao.student.FamilyDao1;
import team.sun.sss.dao.student.StudentDao;
import team.sun.sss.pojo.staff.Staff;
import team.sun.sss.pojo.user.User;
import team.sun.sss.pojo.student.Family;
import team.sun.sss.pojo.hardwork.School;
import team.sun.sss.pojo.student.Student1;
import team.sun.sss.service.student.StudentService;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class StudentController {
    @Autowired
    StudentDao studentDao;
    @Autowired
    FamilyDao1 familyDao1;
    @Autowired
    StaffDao staffDao;
    @Autowired
    StudentService studentService;

    @GetMapping("/student_welcome")
    public String studentWelcome() {
        return "student/student_welcome";
    }
    @GetMapping("/instrutor_welcome")
    public String instructorWelcome() {
        return "student/instructor_welcome";
    }
    @GetMapping("/staff_welcome")
    public String staffWelcome() {
        return "hardWork/staff_welcome";
    }
    @GetMapping("/unit_welcome")
    public String unitWelcome() {
        return "hardWork/unit_welcome";
    }
    //1.学生个人中心
    @GetMapping("/student_info")
    public String studentInfo(Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginInfo");
        model.addAttribute("studentinfo1", studentDao.selectStuById(user.getUserId()));
        model.addAttribute("familyinfo1", familyDao1.getFamilyList(user.getUserId()));
        return "student/student_info";
    }
    @GetMapping("/to_student_update")
    public String to_student_update2(HttpSession session, Model model) {
        User user = (User) session.getAttribute("loginInfo");
        model.addAttribute("studentinfo2", studentDao.selectStuById(user.getUserId()));
        model.addAttribute("familyinfo2", familyDao1.getFamilyList(user.getUserId()));
        return "student/student_update";
    }

    //2学工学生预览表
    @RequestMapping("/staff_manage_stu")
    public String updateStudent(Model model,@RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                                @RequestParam(defaultValue="5",value="pageSize")Integer pageSize) {
        if(pageNum == null){
            pageNum = 1;   //设置默认当前页
        }
        if(pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 5;    //设置默认每页显示的数据数
        }
        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum,pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<Student1> studentList = studentService.getStudentAll();
            model.addAttribute("AllStudentList", studentList);
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            PageInfo<Student1> pageInfo = new PageInfo<Student1>(studentList,pageSize);
            //4.使用model/map/modelandview等带回前端
            model.addAttribute("pageInfo",pageInfo);
        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        return "student/staff_stu_manage";
    }

    @RequestMapping("/staff_select_stu")
    public String selectStudent(Model model, String stuGrade, String province, String city, String stuClass,
                                String stuName, Integer stuId) {
        if ( stuId != null) {
            model.addAttribute("AllStudentList", studentDao.selectStuByStuId(stuId));
        } else if (stuGrade.equals("选择年级") && province.equals("0") && city.equals("0") && stuClass.equals("选择班级") &&
                stuName != null && stuId == null) {
            model.addAttribute("AllStudentList", studentDao.selectStuByName(stuName));
        } else {
            model.addAttribute("AllStudentList", studentDao.selectStuByAll(stuGrade, province, city, stuClass));
        }
        return "student/staff_stu_manage2";
    }

    @GetMapping("/staff_stu_detail/{id}")
    public String staffStuDetail(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("studentinfo3", studentDao.selectStuById(id));
        model.addAttribute("familyinfo3", familyDao1.getFamilyList(id));
        return "student/staff_stu_detail";
    }
    @GetMapping("/to_staff_stu_update/{id}")
    public String toStaffUpdateStu(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("studentInfo4", studentDao.StuById(id));
        model.addAttribute("familyInfo4", familyDao1.getFamilyList(id));
        return "student/staff_stu_update";
    }
    //3.辅导员
    @RequestMapping("/instructor_manage_stu")
    public String instructorManageStu(Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginInfo");
        Staff staff = staffDao.getStaffByName(user.getUserId());
        model.addAttribute("instructorStuInfo", studentDao.selectStuByInstructor(staff.getStaName()));
        model.addAttribute("major", studentDao.selectStuByMajor(staff.getStaName()));
        return "student/instructor_stu_manage";
    }
    @RequestMapping("/instructor_select_class")
    public String instructorSelectClass(Model model, HttpSession session,String major,String stuName, Integer stuId) {
        User user = (User) session.getAttribute("loginInfo");
        Staff staff = staffDao.getStaffByName(user.getUserId());
        if(major.equals("全部")){
            model.addAttribute("instructorStuInfo", studentDao.selectStuByInstructor(staff.getStaName()));
        }else if(major.equals("--请选择所带专业--")&&(stuName!=null)&&(stuId==null)){
            model.addAttribute("instructorStuInfo", studentDao.selectStuByName(stuName));
        }else if(stuId!=null) {
            model.addAttribute("instructorStuInfo", studentDao.selectStuByStuId(stuId));
        }else{
            model.addAttribute("instructorStuInfo", studentDao.selectStuMajor(staff.getStaName(),major));
        }
        model.addAttribute("major", studentDao.selectStuByMajor(staff.getStaName()));
        return "student/instructor_stu_manage";
    }
    @GetMapping("/instructor_stu_detail/{id}")
    public String instructorStuDetail(@PathVariable("id") Integer id, Model model) {
        //查出原来的数据
        model.addAttribute("studentinfo5", studentDao.selectStuById(id));
        model.addAttribute("familyinfo5", familyDao1.getFamilyList(id));
        return "student/instructor_stu_detail";
    }

    @GetMapping("/to_instructor_update_stu/{id}")
    public String toInstructorUpdateStu(@PathVariable("id") Integer id, Model model) {
        //获取某个学生信息
        model.addAttribute("studentinfo6", studentDao.StuById(id));
        //获取家长信息
        model.addAttribute("familyinfo6", familyDao1.getFamilyList(id));
        return "student/instructor_stu_update";
    }
    @RequestMapping("/instructor_major_list/{major}")
    public String instructorMajorList( @PathVariable("major") String major,Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginInfo");
        Staff staff = staffDao.getStaffByName(user.getUserId());
        //将本专业的并且是本辅导员的输出
        model.addAttribute("instructorStuInfo", studentDao.selectStuMajor(staff.getStaName(),major));
        model.addAttribute("major", studentDao.selectStuByMajor(staff.getStaName()));
        return "student/instructor_stu_manage";
    }

}